# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=powershell
pkgver=7.3.10
pkgrel=0
pkgdesc="A cross-platform automation and configuration tool/framework"
arch="x86_64 aarch64 armv7" # blocked by dotnet runtime
url="https://github.com/PowerShell/PowerShell"
license="MIT"
depends="
	dotnet7-runtime
	libpsl-native
	"
makedepends="
	dotnet7-sdk
	git
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/PowerShell/PowerShell/archive/v$pkgver.tar.gz
	dependency-gatherer.targets
	fix-filesystem-test.patch
	update-sdk-test-ppc64le.patch
	"
builddir="$srcdir/PowerShell-$pkgver"
# if true, then within pipeline environment, in which case send logs there
# to be scooped
if [ -d "$APORTSDIR/logs" ]; then
	_logdir="$APORTSDIR"/logs
else
	_logdir="$srcdir"/logs
fi

# mono-flavored runtime does not support crossgen2
case $CARCH in
	armhf|s390x|ppc64le) _use_crossgen2=false;;
esac

# secfixes:
#   7.3.10-r0:
#     - CVE-2023-36013

prepare() {
	default_prepare
	# build expects to be in git directory
	git init
	git config user.name "example"
	git config user.email "example@example.com"
	git add ./*
	git commit -m 'Initial'
	git tag -a "v$pkgver" -m "Initial"
	rm "global.json"
	mkdir -p "$_logdir"
}

build() {
	msg "Building $pkgname"
	# change nuget cache
	export NUGET_PACKAGES="$srcdir"/nuget_cache

	# Restore
	dotnet restore src/powershell-unix
	dotnet restore src/ResGen
	dotnet restore src/TypeCatalogGen

	# Setup the build target to gather dependency information
	cp "$srcdir/dependency-gatherer.targets" \
		"src/Microsoft.PowerShell.SDK/obj/Microsoft.PowerShell.SDK.csproj.TypeCatalog.targets"
	dotnet msbuild src/Microsoft.PowerShell.SDK/Microsoft.PowerShell.SDK.csproj \
		/t:_GetDependencies \
		"/property:DesignTimeBuild=true;_DependencyFile=$PWD/src/TypeCatalogGen/powershell.inc" \
		/nologo

	# Generate 'powershell.version'
	echo "v$pkgver" > powershell.version

	# Generate resource binding C# files
	cd "$builddir"/src/ResGen
	dotnet run

	# Generate 'CorePsTypeCatalog.cs'
	cd "$builddir"/src/TypeCatalogGen
	dotnet run ../System.Management.Automation/CoreCLR/CorePsTypeCatalog.cs powershell.inc

	# Build powershell core
	cd "$builddir"

	dotnet build-server shutdown
	dotnet publish --configuration Linux "src/powershell-unix/" \
		--output bin \
		--no-self-contained \
		--runtime "$(dotnet --info | awk '$1=="RID:"{print $2}')" \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-build.binlog \
		/consoleLoggerParameters:ShowTimestamp
}

check() {
	msg "Checking $pkgname"
	cd "$builddir"/test/xUnit
	dotnet build-server shutdown
	dotnet test \
		-c Release \
		--runtime "$(dotnet --info | awk '$1=="RID:"{print $2}')" \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-check.binlog \
		/consoleLoggerParameters:ShowTimestamp
}

package() {
	# directory creation
	install -dm 755 \
		"$pkgdir"/usr/lib \
		"$pkgdir"/usr/bin

	# libary copy
	cp -ar "$builddir"/src/powershell-unix/bin/Linux/*/alpine* "$pkgdir"/usr/lib/$pkgname

	# already provided by 'libpsl-native' aport
	rm -f "$pkgdir"/usr/lib/$pkgname/libpsl-native.so

	# does not build for linux-musl, thus points to libc.so rather than musl
	# see https://github.com/dotnet/runtime/issues/63187
	rm -f "$pkgdir"/usr/lib/$pkgname/libSystem.IO.Ports.Native.so

	# binary link
	ln -s "/usr/lib/$pkgname/pwsh" "$pkgdir"/usr/bin/pwsh
}

sha512sums="
9876ed7942102293bd55cfaa126b3801603a759237c809ae9d56315258e97aae2658522add43ae32abc217bd5e5e20078042b3c949ee2e9c5182c63915ad792d  powershell-7.3.10.tar.gz
e9c4aeebd4fead1542b2c48c70134efcdb6c573273d8d05b1f8a2ece070e26899876b30264749992beac3044059716ca8c07b3bf970a419e525057c422f843c6  dependency-gatherer.targets
0e7550d8b702eeb9ebbd42100b32cbf7a0be676c5922cd6ee03ecdd3ba11d20d42a468f73ee428c6c92c8b7b18f9b022591a368191c2a358034ba4d7e78d2b84  fix-filesystem-test.patch
1aa10d32a37da0e7fd9d587105833d0a7ed9b71701018259d3efa2be90977808eb54a7d3e2d8bf03b693eeeac469f2e9ee88aa93006b7a2c22ae4720a8f62561  update-sdk-test-ppc64le.patch
"
